home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Best of MacTutor - S…e Code for Volumes 1 to 5
/
The Best of MacTutor - Source Code for Volume 1-5 (Wayzata Technology)(6031)(1990).bin
/
Source Code
/
#39 (Dec 88)
/
Twindow
/
TWindowTester.c
< prev
next >
Wrap
C/C++ Source or Header
|
1988-04-29
|
18KB
|
942 lines
/*
* TWindowTester
*
* C source of a simple program to demonstrate usage of tool windows by
* using routines from the TWindow Manager. Tool windows are windows
* that always float on top, typically for palettes and tools.
*
* Thomas Fruin 1988
*
* fruin@hlerul5.BITNET University of Leiden
* thomas@uvabick.UUCP University of Amsterdam
* dibs@well.UUCP
* hol0066.AppleLink
* 2:508/15.FidoNet The Netherlands
*
* TWindowTester is based on MiniEdit - Mini text editor,converted from
* the listing in Macintosh Revealed, vol II. As little as possible was
* modified in the original program, although most of the functionality
* is no longer there.
*/
#include <Types.h>
#include <QuickDraw.h>
#include <Fonts.h>
#include <Windows.h>
#include <Events.h>
#include <TextEdit.h>
#include <Controls.h>
#include <Dialogs.h>
#include <Menus.h>
#include <Memory.h>
#include <OSUtils.h>
#include <ToolUtils.h>
#include <Desk.h>
#include <TWindows.h>
#define MenuBarHeight 20
#define TitleBarHeight 18
#define ScreenMargin 4
#define MinWidth 80
#define MinHeight 80
#define SBarWidth 16
#define AppleID 1
#define AboutItem 1
#define FileID 2
#define NewItem 1
#define NewToolItem 2
#define CloseItem 3
#define QuitItem 5
#define EditID 3
#define UndoItem 1
#define CutItem 3
#define CopyItem 4
#define PasteItem 5
#define ClearItem 7
#define WindowsID 4
#define ToolsID 5
#define AboutID 128
#define windowID 1000
#define scrollID 1000
#define toolID 1001
#define okButton 1
#define aboutBold 8
#define wHOffset 20
#define wVOffset 20
#define tHOffset 40
#define tVOffset 40
#define undoCmd 0
#define cutCmd 2
#define copyCmd 3
#define pasteCmd 4
#define clearCmd 5
/* z-ordered list of windows (nearest first) */
#define windowList (*(WindowPeek *)0x9D6)
#define nilproc ((ProcPtr)0)
struct WindowData
{
ControlHandle scBar;
};
typedef struct WindowData WindowData;
typedef struct WindowData *WDPtr;
typedef struct WindowData **WDHandle;
MenuHandle AppleMenu,
FileMenu,
EditMenu,
WindowsMenu,
ToolsMenu;
INTEGER toolCount,
windowCount,
lastToolsItem,
lastWindowsItem;
Boolean Finished,
ErrorFlag;
main()
{
Initialize();
do
{
if ( FrontWindow() == nil )
DisableItem( FileMenu, CloseItem );
SystemTask();
DoEvent();
}
while ( !Finished );
}
Initialize()
{
INTEGER theMask;
InitGraf( &qd.thePort );
InitFonts();
InitWindows();
InitMenus();
TEInit();
InitDialogs( nilproc );
TInitWindows();
theMask = everyEvent - keyUpMask;
SetEventMask( theMask );
FlushEvents( everyEvent, 0 );
SetUpMenus();
InitCursor();
}
SetUpMenus()
{
AppleMenu = GetMenu( AppleID );
AddResMenu( AppleMenu, 'DRVR' );
InsertMenu( AppleMenu, 0 );
FileMenu = GetMenu( FileID );
InsertMenu( FileMenu, 0 );
EditMenu = GetMenu( EditID );
InsertMenu( EditMenu, 0 );
WindowsMenu = GetMenu( WindowsID );
InsertMenu( WindowsMenu, 0 );
ToolsMenu = GetMenu( ToolsID );
InsertMenu( ToolsMenu, 0 );
DrawMenuBar();
lastToolsItem =
lastWindowsItem = 0;
}
DoEvent()
{
EventRecord theEvent;
ErrorFlag = false;
if ( TGetNextEvent( everyEvent, &theEvent ))
{
switch ( theEvent.what )
{
case mouseDown:
DoMouseDown( &theEvent );
break;
case keyDown:
case autoKey:
DoKeyStroke( &theEvent );
break;
case updateEvt:
DoUpdate( &theEvent );
break;
case activateEvt:
DoActivate( &theEvent );
break;
default:
break;
}
}
}
DoMouseDown( theEvent )
EventRecord *theEvent;
{
WindowPtr whichWindow;
INTEGER thePart;
thePart = FindWindow( &theEvent->where, &whichWindow );
switch ( thePart )
{
case inDesk:
break;
case inMenuBar:
DoMenuClick( theEvent );
break;
case inSysWindow:
SystemClick( theEvent, whichWindow );
break;
case inContent:
DoContent( theEvent, whichWindow );
break;
case inDrag:
DoDrag( theEvent, whichWindow );
break;
case inGrow:
DoGrow( theEvent, whichWindow );
break;
case inGoAway:
DoGoAway( theEvent, whichWindow );
break;
}
}
DoMenuClick( theEvent )
EventRecord *theEvent;
{
LONGINT menuChoice;
menuChoice = MenuSelect( &theEvent->where );
DoMenuChoice( menuChoice );
}
DoMenuChoice( menuChoice )
LONGINT menuChoice;
{
INTEGER theMenu,
theItem;
if ( menuChoice )
{
theMenu = HiWord( menuChoice );
theItem = LoWord( menuChoice );
switch (theMenu)
{
case AppleID:
DoAppleChoice( theItem );
break;
case FileID:
DoFileChoice( theItem );
break;
case EditID:
DoEditChoice( theItem );
break;
case WindowsID:
DoWindowsChoice( WindowsMenu, theItem );
break;
case ToolsID:
DoWindowsChoice( ToolsMenu, theItem );
break;
}
HiliteMenu( 0 );
}
}
DoAppleChoice( theItem )
INTEGER theItem;
{
char accName[255];
INTEGER accNumber;
switch ( theItem )
{
case AboutItem:
DoAbout();
break;
default:
EnableItem( FileMenu, CloseItem );
EnableItem( EditMenu, UndoItem );
EnableItem( EditMenu, CutItem );
EnableItem( EditMenu, CopyItem );
EnableItem( EditMenu, PasteItem );
EnableItem( EditMenu, ClearItem );
GetItem( AppleMenu, theItem, accName );
accNumber = OpenDeskAcc( accName );
break;
}
}
DoAbout()
{
EventRecord theEvent;
DialogPtr aboutDialog;
INTEGER theItem;
void SetBold();
aboutDialog = GetNewDialog( AboutID, nil, inFront );
SetBold( aboutDialog, aboutBold );
TShowWindow( aboutDialog );
if ( TGetNextEvent( activMask, &theEvent ))
DoActivate( &theEvent );
if ( TGetNextEvent( activMask, &theEvent ))
DoActivate( &theEvent );
ModalDialog( nilproc, &theItem );
THideWindow ( aboutDialog );
DisposDialog( aboutDialog );
}
DoFileChoice( theItem )
INTEGER theItem;
{
switch (theItem)
{
case NewItem:
DoNew();
break;
case NewToolItem:
DoNewTool();
break;
case CloseItem:
DoClose();
break;
case QuitItem:
DoQuit();
break;
}
}
DoNew()
{
WindowPtr theWindow;
WDHandle theData;
char title[ 255 ];
theWindow = TGetNewWindow( userKind, windowID, nil, inFront );
OffsetWindow( theWindow, wHOffset, wVOffset, &windowCount );
TShowWindow ( theWindow );
SetPort( theWindow );
theData = ( WDHandle )NewHandle(( Size )sizeof( WindowData ));
SetWRefCon( theWindow, ( LONGINT )theData );
HLock( theData );
( *theData )->scBar = GetNewControl( scrollID, theWindow );
HUnlock( theData );
EnableItem( FileMenu, CloseItem );
GetWTitle( theWindow, title );
InsMenuItem( WindowsMenu, title, lastWindowsItem++ );
CheckItem ( WindowsMenu, lastWindowsItem, true );
}
DoNewTool()
{
WindowPtr toolWindow;
char title[ 255 ];
toolWindow = TGetNewWindow( toolKind, toolID, nil, inFront );
OffsetWindow( toolWindow, tHOffset, tVOffset, &toolCount );
TShowWindow( toolWindow );
SetPort( toolWindow );
EnableItem( FileMenu, CloseItem );
GetWTitle( toolWindow, title );
InsMenuItem( ToolsMenu, title, lastToolsItem++ );
CheckItem ( ToolsMenu, lastToolsItem, true );
}
OffsetWindow( whichWindow, hOffset, vOffset, count )
WindowPtr whichWindow;
INTEGER hOffset,
vOffset,
*count;
{
INTEGER windowWidth,
windowHeight,
hExtra,
vExtra,
hMax,
vMax,
windowLeft,
windowTop;
Rect pRect;
char title [ 255 ],
number[ 3 ];
pRect = whichWindow->portRect;
windowWidth = pRect.right - pRect.left;
windowHeight = pRect.bottom - pRect.top;
windowHeight += TitleBarHeight;
hExtra = qd.screenBits.bounds.right - windowWidth;
vExtra = qd.screenBits.bounds.bottom - ( windowHeight + MenuBarHeight );
hMax = ( hExtra / hOffset ) + 1;
vMax = ( vExtra / vOffset ) + 1;
++*count;
windowLeft = ( *count % hMax ) * hOffset;
windowTop = ( *count % vMax ) * vOffset;
windowTop = windowTop + TitleBarHeight + MenuBarHeight;
MoveWindow( whichWindow, windowLeft, windowTop, false );
GetWTitle( whichWindow, title );
NumToString(( LONGINT )*count, number );
SetWTitle( whichWindow, strcat( title, number ));
}
DoClose()
{
WindowPtr whichWindow;
INTEGER theWKind;
whichWindow = TFrontWindow( userKind );
if ( whichWindow != nil )
{
theWKind = TGetWKind( whichWindow );
switch( theWKind )
{
case userKind:
CloseDocWindow( whichWindow );
break;
case systemKind:
CloseSysWindow ( whichWindow );
break;
}
}
else
{
whichWindow = TFrontWindow( toolKind );
if ( whichWindow != nil )
CloseToolWindow( whichWindow );
}
}
CloseDocWindow( whichWindow )
WindowPtr whichWindow;
{
WDHandle theData;
INTEGER ItemOf();
EventRecord theEvent;
lastWindowsItem--;
DelMenuItem( WindowsMenu, ItemOf( whichWindow, WindowsMenu ));
theData = ( WDHandle )GetWRefCon( whichWindow );
THideWindow( whichWindow );
if ( TGetNextEvent( activMask, &theEvent ))
DoActivate( &theEvent );
if ( TGetNextEvent( activMask, &theEvent ))
DoActivate( &theEvent );
if ( theData != nil )
DisposHandle( theData );
TDisposeWindow( whichWindow );
}
CloseToolWindow( whichWindow )
WindowPtr whichWindow;
{
INTEGER ItemOf();
lastToolsItem--;
DelMenuItem( ToolsMenu, ItemOf( whichWindow, ToolsMenu ));
TDisposeWindow( whichWindow );
}
CloseSysWindow( whichWindow )
WindowPtr whichWindow;
{
INTEGER accNumber;
accNumber = (( WindowPeek )whichWindow )->windowKind;
CloseDeskAcc( accNumber );
}
DoQuit()
{
Finished = true;
}
DoEditChoice( theItem )
INTEGER theItem;
{
switch ( theItem )
{
case UndoItem:
if ( !SystemEdit( undoCmd ))
DoUndo();
break;
case CutItem:
if ( !SystemEdit( cutCmd ))
DoCut();
break;
case CopyItem:
if ( !SystemEdit( copyCmd ))
DoCopy();
break;
case PasteItem:
if ( !SystemEdit( pasteCmd ))
DoPaste();
break;
case ClearItem:
if ( !SystemEdit(clearCmd ))
DoClear();
break;
}
}
DoUndo()
{
SysBeep(1);
}
DoCut()
{
SysBeep(1);
}
DoCopy()
{
SysBeep(1);
}
DoPaste()
{
SysBeep(1);
}
DoClear()
{
SysBeep(1);
}
DoWindowsChoice( theMenu, theItem )
MenuHandle theMenu;
INTEGER theItem;
{
INTEGER theMark;
WindowPtr whichWindow,
WindowOf();
whichWindow = WindowOf( theMenu, theItem );
GetItemMark( theMenu, theItem, &theMark );
if ( theMark == noMark )
{
CheckItem( theMenu, theItem, true );
TShowWindow( whichWindow );
}
else
{
CheckItem( theMenu, theItem, false );
THideWindow( whichWindow );
}
}
DoContent( theEvent, whichWindow )
EventRecord *theEvent;
WindowPtr whichWindow;
{
INTEGER theWKind;
theWKind = TGetWKind( whichWindow );
if ( theWKind == userKind )
{
if ( whichWindow != TFrontWindow( userKind ))
TSelectWindow( whichWindow );
}
else if ( theWKind == toolKind )
{
if ( whichWindow != TFrontWindow( toolKind ))
TSelectWindow( whichWindow );
/* Only if the window has no title bar */
DoDrag( theEvent, whichWindow );
}
}
DoDrag( theEvent, whichWindow)
EventRecord *theEvent;
WindowPtr whichWindow;
{
Rect limitRect;
SetRect( &limitRect, 0, MenuBarHeight,
qd.screenBits.bounds.right,
qd.screenBits.bounds.bottom );
InsetRect( &limitRect, ScreenMargin, ScreenMargin );
TDragWindow( whichWindow, theEvent, &limitRect );
}
DoGrow( theEvent, whichWindow )
EventRecord *theEvent;
WindowPtr whichWindow;
{
Rect sizeRect;
LONGINT newSize;
INTEGER newWidth,
newHeight;
GrafPtr savePort;
if ( whichWindow != TFrontWindow( userKind ))
TSelectWindow( whichWindow );
else
{
SetRect( &sizeRect, MinWidth, MinHeight,
qd.screenBits.bounds.right,
qd.screenBits.bounds.bottom - MenuBarHeight );
newSize = GrowWindow( whichWindow, &theEvent->where, &sizeRect );
if ( newSize )
{
GetPort( &savePort );
SetPort( whichWindow );
EraseRect( &whichWindow->portRect );
newWidth = LoWord( newSize );
newHeight = HiWord( newSize );
SizeWindow( whichWindow, newWidth, newHeight, true );
InvalRect( &whichWindow->portRect );
FixScrollBar( whichWindow );
SetPort( savePort );
}
}
}
FixScrollBar( whichWindow )
WindowPtr whichWindow;
{
WDHandle theData;
ControlHandle theScrollBar;
Rect pRect;
theData = ( WDHandle )GetWRefCon( whichWindow );
if ( theData != nil )
{
theScrollBar = ( *theData )->scBar;
HideControl( theScrollBar );
pRect = whichWindow->portRect;
MoveControl( theScrollBar, pRect.right - ( SBarWidth - 1 ), -1 );
SizeControl( theScrollBar, SBarWidth, ( pRect.bottom + 1 )
- ( pRect.top - 1 ) - ( SBarWidth - 1 ));
ShowControl( theScrollBar );
ValidRect( &( *theScrollBar )->contrlRect );
}
}
DoGoAway( theEvent, whichWindow )
EventRecord *theEvent;
WindowPtr whichWindow;
{
INTEGER theWKind;
theWKind = TGetWKind( whichWindow );
if ( theWKind == userKind )
{
if ( whichWindow != TFrontWindow( userKind ))
TSelectWindow( whichWindow );
else if ( TrackGoAway( whichWindow, &theEvent->where ))
CloseDocWindow( whichWindow );
}
else if ( theWKind == toolKind )
{
if ( TrackGoAway( whichWindow, &theEvent->where ))
{
lastToolsItem--;
DelMenuItem( ToolsMenu, ItemOf( whichWindow, ToolsMenu ));
TDisposeWindow( whichWindow );
}
}
}
DoKeyStroke( theEvent )
EventRecord *theEvent;
{
INTEGER ch;
LONGINT menuChoice;
ch = ( INTEGER )( theEvent->message & charCodeMask );
if ( theEvent->modifiers & cmdKey )
{
if ( theEvent->what != autoKey )
{
menuChoice = MenuKey( ch );
DoMenuChoice( menuChoice );
}
}
}
DoUpdate( theEvent )
EventRecord *theEvent;
{
GrafPtr savePort;
WindowPtr whichWindow;
WDHandle theData;
INTEGER theWKind;
GetPort( &savePort );
whichWindow = ( WindowPtr )theEvent->message;
theWKind = TGetWKind( whichWindow );
SetPort( whichWindow );
BeginUpdate( whichWindow );
if ( theWKind == userKind )
{
EraseRect( &whichWindow->portRect );
if ( GetWRefCon( whichWindow ) != nil )
{
DrawGrowIcon( whichWindow );
DrawControls( whichWindow );
}
}
else if ( theWKind == toolKind )
{
EraseRect( &whichWindow->portRect );
}
EndUpdate( whichWindow );
SetPort( savePort );
}
DoActivate( theEvent )
EventRecord *theEvent;
{
WindowPtr whichWindow,
theFrontWindow;
WDHandle theData;
INTEGER theWKind,
frontWKind;
whichWindow = ( WindowPtr )theEvent->message;
SetPort( whichWindow );
theWKind = TGetWKind( whichWindow );
if ( theWKind == userKind )
{
theData = (WDHandle )GetWRefCon( whichWindow );
if ( theData != nil )
{
HLock( theData );
if ( theEvent->modifiers & activeFlag )
{
ShowControl(( *theData )->scBar );
DisableItem( EditMenu, UndoItem );
DisableItem( EditMenu, CutItem );
DisableItem( EditMenu, CopyItem );
DisableItem( EditMenu, PasteItem );
DisableItem( EditMenu, ClearItem );
}
else
{
HideControl(( *theData )->scBar );
theFrontWindow = FrontWindow();
frontWKind = TGetWKind( theFrontWindow );
if ( frontWKind == systemKind )
{
EnableItem( EditMenu, UndoItem );
EnableItem( EditMenu, CutItem );
EnableItem( EditMenu, CopyItem );
EnableItem( EditMenu, PasteItem );
EnableItem( EditMenu, ClearItem );
}
}
HUnlock( theData );
DrawGrowIcon( whichWindow );
}
}
}
INTEGER
ItemOf( whichWindow, whichMenu )
WindowPtr whichWindow;
MenuHandle whichMenu;
{
char theTitle[ 255 ],
theMenu [ 255 ];
Boolean done;
INTEGER theItem;
GetWTitle( whichWindow, theTitle );
for ( done = false,
theItem = 1;
done == false;
theItem++ )
{
GetItem( whichMenu, theItem, theMenu );
if ( strcmp( theTitle, theMenu ) == 0 )
{
done = true;
theItem--;
}
}
return( theItem );
}
WindowPtr
WindowOf( whichMenu, whichItem )
MenuHandle whichMenu;
INTEGER whichItem;
{
char theTitle[ 255 ],
theMenu [ 255 ];
Boolean done;
INTEGER wantedKind,
theWKind;
WindowPeek loopWindow,
wantedWindow;
GetItem( whichMenu, whichItem, theMenu );
if ( whichMenu == WindowsMenu )
wantedKind = userKind;
else
wantedKind = toolKind;
for ( done = false,
loopWindow =
wantedWindow = windowList;
done == false && loopWindow != nil;
loopWindow = loopWindow->nextWindow )
{
theWKind = TGetWKind( loopWindow );
if ( theWKind == wantedKind )
{
GetWTitle( loopWindow, theTitle );
if ( strcmp( theTitle, theMenu ) == 0 )
{
wantedWindow = loopWindow;
done = true;
}
}
}
return(( WindowPtr )wantedWindow );
}
void
SetBold( theDialog, bolditem )
DialogPtr theDialog;
short bolditem;
{
INTEGER itemType;
Handle itemHandle;
Rect itemRect;
pascal void OutlineButton();
/* Get the OK button's display rectangle, enlarge the
rectangle, and set up the useritem with this rectangle
and a pointer to the OutlineButton function. */
GetDItem( theDialog, okButton, &itemType, &itemHandle, &itemRect );
InsetRect( &itemRect, -3, -3 );
SetDItem( theDialog, bolditem, userItem,
( Handle )OutlineButton, &itemRect );
}
pascal void
OutlineButton( theDialog, theItem )
DialogPtr theDialog;
INTEGER theItem;
{
INTEGER itemType;
Handle itemHandle;
Rect itemRect;
PenState savePen;
/* Get the bounding rectangle of the OK button item
and boldly outline it. */
GetDItem( theDialog, okButton, &itemType, &itemHandle, &itemRect );
GetPenState( &savePen );
PenSize( 3,3 );
InsetRect( &itemRect, -4, -4 );
FrameRoundRect( &itemRect, 16, 16 );
SetPenState( &savePen );
}